
var access_token = window.localStorage.token
sourceList()
function sourceList() {
    "use strict"
    $.ajax({
        type:'post',
        url:api('/user/get-source'),
        data:{
            access_token:access_token,
        },
        dataType:'json',
        success:function (res) {
            console.log(res.data.list)
            viewSource(res.data.list)

        }
    })
}
function viewSource(source) {
    var html = ''
    $.each(source,function (index,item) {
        console.log(item.name)
        html +=`<option value="${item.id}">${item.name}</option>`
    })
    $('.form-control').html(html)
}
function saveHandle(){
    var sourceId = $('.source')[0].value
    console.log(sourceId)
    var userData = {}
    var identity = $(".identity_num")[0].value
    var phone = $(".phone")[0].value
    var phoneVerifg = /^1[34578]\d{9}$/;
    var identityVerifg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
    if($(".name")[0].value == ''){
        $('.nameHint').text('姓名不能为空').css("color","red")
        return false;
    }else {
        userData.name = $(".name")[0].value
        $('.nameHint').hide()
    }
    if(identity == ''){
        $('.identityHint').text('身份证不能为空').css("color","red")
        return false;
    }else if(!identityVerifg.test(identity)){
        $('.identityHint').text('身份证格式不正确').css("color","red")
        return false;
    }
    else {
        userData.identity_num = identity
        $('.identityHint').hide()
    }
    if(phone == ''){
        $('.phoneHint').text('手机号不能为空').css("color","red")
        return false;
    }else if(!phoneVerifg.test(phone)){
        $('.phoneHint').text('手机号格式不正确').css("color","red")
        return false;
    }else {
        userData.phone = phone
        $('.phoneHint').hide()
    }
    userData.access_token = access_token
    userData.isHorseman = '1'
    userData.source_id = sourceId
    console.log(userData)
    $.ajax({
        type: "POST",
        url: api('/user/update'),
        data: userData,
        dataType: "json",
        success: function (res) {
            console.log(res)
            if(res.success){
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                window.location.href = './home.html?v='+ random +''
            }else {
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        },

    });
}
